package FigureDogs.anews24;

import java.io.IOException;
import java.net.URL;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class Anews24Main extends Activity {
	WebView mWebView,mWebView2;
	TextView titletv;
	public Document doc;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.anews24_main);
		new parser().execute();
		
		ImageButton news=(ImageButton)findViewById(R.id.ButtonNews);
		news.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	Intent i=new Intent(Anews24Main.this, NewsActivity.class);
                startActivity(i);									
                    
            }
        });
	}
	
	class parser extends AsyncTask<Void, Void, Void> {
        Document doc;

		@Override
		protected Void doInBackground(Void... arg0) { 
		        Document doc = null;
		        try {
		            doc = Jsoup.connect("http://www.anews24.it/").get();
		        } catch (IOException e) {
		            // TODO Auto-generated catch block
		            e.printStackTrace();
		        }

		        Element featured =doc.getElementById("featured");
		       // Element content = doc.getElementById("content");
		        Elements links = featured.getElementsByTag("img");
		        String linkHref=null;
		        for (Element link : links) {
		           linkHref = link.attr("src");
		          String linkText = link.text();
//		         System.out.println(linkHref);
		        }
		       
		        mWebView = (WebView) findViewById(R.id.webview);
		        mWebView.setWebViewClient(new NewsClient());
		        mWebView.setInitialScale(60);
		        mWebView.setScrollbarFadingEnabled(true);
		        WebSettings webSettings = mWebView.getSettings();
//		        webSettings.setUseWideViewPort(true);
//		        webSettings.setLoadWithOverviewMode(true);
		        mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		        mWebView.loadUrl(linkHref);
		        mWebView2=(WebView) findViewById(R.id.WebView01);
		        Elements elements2=featured.getElementsByTag("h2");
		        String testo = elements2.text();
		        mWebView2.loadDataWithBaseURL("","<html><head><style type='text/css'>html,body {margin: 0;padding: 0;width: 100%;height: 100%;}html {display: table;}body {display: table-cell;vertical-align: middle;text-align: center;font-weight:bold;}</style></head><body><p>"+testo +"</p></body></html>","text/html", "utf-8", null);
				return null;
		}


    }

 


private class NewsClient extends WebViewClient {
	
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    	System.out.println("URL: " + url);
    	view.loadUrl("javascript:changeLocation('" + url + "')");
        return true;
    }
}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.anews24_main, menu);
		return true;
	}

}
